package com.neuedu.service.impl;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.api.FileUploadService;
import com.neuedu.entity.Employee;
import com.neuedu.exception.NeueduException;
import com.neuedu.mapper.EmployeeMapper;
import com.neuedu.mapper.SchedulingMapper;
import com.neuedu.service.EmployeeService;
import com.neuedu.util.JwtUtil;
import jakarta.annotation.Resource;
import net.sf.jsqlparser.expression.operators.relational.Matches;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author LiYang
 * @since 2024-11-07
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Resource
    FileUploadService fileUploadService;
    @Resource
    PasswordEncoder passwordEncoder;
    @Resource
    SchedulingMapper schedulingMapper;
    @Resource(name = "wyredis")
    RedisTemplate<String,Object> redisTemplate;
    @Override
    public IPage<Employee> list(Integer pageNo, Integer pageSize, String name) {
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(Employee::getRealname,name);
        }
        return this.page(new Page<>(pageNo,pageSize),lambdaQueryWrapper);
    }
    @Override
    public boolean check(String field, String value, Long id) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq(field, value);
        if (null != id) {
            wrapper.ne("id", id);
        }
        return this.count(wrapper) == 0;
    }

    @Override
    public boolean add(Long deptmentId, Long registLevelId, Long schedulingId, String realname, String phone, String email, String password, MultipartFile file) {

        return this.save(new Employee(
                deptmentId,
                registLevelId,
                schedulingId,
                realname,
                phone,
                email,
                fileUploadService.upload(file),
                passwordEncoder.encode(password) // 密码加密
        ));
    }
    @Override
    public List<Employee> getRegisterEmp(Integer week, String noon, Long deptId, Long registLevelId) {
        /**
         *  判断字符串 尽量这么写"AM".equals(noon),如果是空对象是空字符串不会报异常
         */
        int weishu = "AM".equals(noon) ? week * 2 + 1 : week * 2 + 2; //找到当前的医生排班的位数 奇数上午，偶数下午
        List<Integer> Schedulings = schedulingMapper.getIdByWeek(weishu);/// 根据星期和上下午判断出医生
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("deptment_id", deptId)
                .eq("regist_level_id", registLevelId)
                .in("scheduling_id", Schedulings);
        return this.list(wrapper);
    }

    @Override
    public String login(String username, String password) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",username);
        Employee employee = this.getOne(wrapper);
        if (null == employee && !passwordEncoder.matches(password,employee.getPassword()) ){
            throw new NeueduException("用户名或密码错误");
        }
        if(!employee.getDelmark()){
            throw  new NeueduException("该用户已经被禁用");
        }
        // 使用redis缓存token,超时时间为30分钟
        redisTemplate.opsForValue().set("login::"+employee.getId(),"login",4, TimeUnit.HOURS);
        return JwtUtil.createToken(employee);
    }

    @Override
    public Employee getByPhonePass(String username, String password) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getPhone,username);
        return this.getOne(wrapper);

    }
}
